﻿package com.kuanter.kuanterauto.utils;

import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

public class StringUtil {
	private char[] CHARS = {  
		 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
		 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',   
		 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',  
		 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',   
		 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'  
		}; 
	int length = CHARS.length;
	public static boolean compareString(String one,String two){
		one = one.trim();
		two = two.trim();
		if(one.equals(two)){
			return true;
		}
		return false;
	}
	
	public String createAuthCode(){
		StringBuilder buffer = new StringBuilder();  
		Random random = new Random();
		for (int i = 0; i < 5; i++) {  
			buffer.append(CHARS[random.nextInt(length)]);  
		}  
		return buffer.toString();  
	}
	

	/**
	 * 判断返回json数据status值是否为true
	 * 
	 * @param result
	 * @return
	 * @throws JSONException
	 */
	public static boolean isSuccess(String result) throws JSONException {
		JSONObject jobj = new JSONObject(result);
		if (!jobj.optString("status").equals("false")) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获得返回数据中 errorMsg 字段值
	 * 
	 * @param result
	 * @return
	 * @throws JSONException
	 */
	public static String geterrorMsg(String result) throws JSONException {
		JSONObject jobj = new JSONObject(result);
		if (!jobj.optString("errorMsg").equals("null")) {
			return jobj.optString("errorMsg");
		} else {
			return "请求失败";
		}
	}

}
